﻿#region Using

using System;
using System.Collections.ObjectModel;
using System.Threading;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XNgine;
using XNgine.ScreenManagement.StandardScreens;
using XNgine.ScreenManagement.StandardScreens.Menu;
using XNgine.Services;
using XNgine.Threading;
using XNgine.Util;

#endregion

namespace XNgine_Test_Game.GameScreens
{
    public class MainMenu : MenuScreen
    {
        public const string SCREEN_NAME = "TestGame.MainMenu";

        public bool PauseMode { get; set; }

        /// <summary>  
        /// A default font for menu items
        /// </summary>  
        public static SpriteFont MenuItemFont { get; set; }

        public MainMenu()
            : this(null)
        {
        }

        public MainMenu(string title)
            : base(SCREEN_NAME, title)
        {
            BlocksDraw = false;
            //BlocksUpdate = false;
        }

        protected override void InitializeMenuItems()
        {
            MenuItem playGame = new MenuItem("Play Game");
            MenuItem options = new MenuItem("Options");
            MenuItem quit = new MenuItem("Quit");

            playGame.Activated += PlayGameActivated;
            options.Activated += OptionsActivated;
            quit.Activated += QuitActivated;

            Items.Add(playGame);
            Items.Add(options);
            Items.Add(quit);

            XEngine.Instance.GetService<IKeyboardDevice>().KeyPressed += MainMenu_KeyPressed;
        }

        void MainMenu_KeyPressed(Collection<Keys> keys)
        {
            if (!IsInputAllowed) return;

            if (keys.Contains(Keys.Escape))
            {
                if (PauseMode)
                    PlayGameActivated(this, null);
            }
        }

        public override void Draw()
        {
            if(PauseMode)
                GraphicsUtil.FadeBackBufferToBlack(100);
            base.Draw();
        }
        private void PlayGameActivated(object sender, EventArgs e)
        {
            if (PauseMode)
                XEngine.Instance.GetService<IScreenManagement>().Show(GameplayScreen.SCREEN_NAME);
            else
                LoadingScreen.Load(true, false, new GameplayScreen());

            //LoadingScreen loadingScreen = new LoadingScreen(true, false, new GameplayScreen());
            //loadingScreen.AdditionalLoading += additional_loading;
            //loadingScreen.Show();
        }

        private static void OptionsActivated(object sender, EventArgs e)
        {
            //ScreenManager.AddScreen(new OptionsMenuScreen());  
        }

        private static void QuitActivated(object sender, EventArgs e)
        {
            const string message = "Are you sure you want to quit?\n";

            MessageBox messageBox = XEngine.Instance.GetService<IScreenManagement>().ShowMessageBox(message);
            messageBox.Accepted += messageBox_Accepted;
        }

        private static void messageBox_Accepted(object sender, EventArgs e)
        {
            XEngine.Instance.Exit();
        }
    }
}